package com.example.demo.service.impl;

import com.example.demo.entity.UnitTest;
import com.example.demo.mapper.UnitTestMapper;
import com.example.demo.service.TestService;
import com.example.demo.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * ClassName:TestServiceImpl
 * Package:com.example.demo.service.impl
 * TODO:
 *
 * @date:2021/9/27 14:30
 * @author:Vicky(陈烟星)
 */
@Service
public class TestServiceImpl implements TestService {
    private static Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);

    private static final String KEY_BASE = "xe_key:%s";

    @Autowired
    private UnitTestMapper unitTestMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public void selectSqlNum() {
        long startTime1 = System.currentTimeMillis();
        List<UnitTest> unitTests1 = unitTestMapper.selectNum(1);
        logger.info("测试查询1条条用时：{} 毫秒", System.currentTimeMillis() - startTime1);

        long startTime10 = System.currentTimeMillis();
        List<UnitTest> unitTests10 = unitTestMapper.selectNum(10);
        logger.info("测试查询10条条用时：{} 毫秒", System.currentTimeMillis() - startTime10);

        long startTime100 = System.currentTimeMillis();
        List<UnitTest> unitTests100 = unitTestMapper.selectNum(100);
        logger.info("测试查询100条条用时：{} 毫秒", System.currentTimeMillis() - startTime100);

        long startTime1000 = System.currentTimeMillis();
        List<UnitTest> unitTests1000 = unitTestMapper.selectNum(1000);
        logger.info("测试查询1000条条用时：{} 毫秒", System.currentTimeMillis() - startTime1000);

        long startTime2000 = System.currentTimeMillis();
        List<UnitTest> unitTests2000 = unitTestMapper.selectNum(2000);
        logger.info("测试查询2000条条用时：{} 毫秒", System.currentTimeMillis() - startTime2000);

        long startTime5000 = System.currentTimeMillis();
        List<UnitTest> unitTests5000 = unitTestMapper.selectNum(5000);
        logger.info("测试查询5000条条用时：{} 毫秒", System.currentTimeMillis() - startTime5000);
    }

    @Override
    public void queryRedisOneKey() {
        long currentTimeMillis = System.currentTimeMillis();
        String key = String.format(KEY_BASE, "one");
        ValueOperations<String, String> stringOperations = redisTemplate.opsForValue();
        String value = stringOperations.get(key);
        if (StringUtils.isBlank(value)) {
            return;
        }
        List<UnitTest> unitTestList = JsonUtil.toJavaBeanList(value, UnitTest.class);
        Random random = new Random();
        int n = random.nextInt(unitTestList.size());
        UnitTest unitTest = unitTestList.get(n);
        String name = unitTest.getName();
        logger.info("取出1个key的500行数据的某个值的字段：key:{},name:{},{} 毫秒",key, name,System.currentTimeMillis() - currentTimeMillis);
        unitTest.setId(1000L);

        redisTemplate.opsForValue().set(key, JsonUtil.toJsonStr(unitTestList));

        logger.info("更新一个key的500行数据的某个值,name:{}：{} 毫秒", name,System.currentTimeMillis() - currentTimeMillis);


    }

    @Override
    public void queryRedisMoreKey() {
        long currentTimeMillis = System.currentTimeMillis();
        ValueOperations<String, String> stringOperations = redisTemplate.opsForValue();
        Random random = new Random();
        int n = random.nextInt(500);

        String key = String.format(KEY_BASE, n);
        String value = stringOperations.get(key);

        if (StringUtils.isBlank(value)) {
            return;
        }
        UnitTest unitTest = JsonUtil.toJavaBean(value, UnitTest.class);
        String name = unitTest.getName();
        logger.info("取出500个key的1行数据的某个值的字段：key:{},name:{},{} 毫秒",key, name,System.currentTimeMillis() - currentTimeMillis);
        unitTest.setId(1000L);
        stringOperations.set(key, JsonUtil.toJsonStr(unitTest));

        logger.info("更新500个key的1行数据的某个值：key:{},{} 毫秒",key, System.currentTimeMillis() - currentTimeMillis);

        String value1 = stringOperations.get(key);
        System.out.println(value1);
    }
}
